
package midas.mw.crt;

import java.util.LinkedList;

public class QueueEmulate 
{
	private LinkedList<Object> q;

	QueueEmulate()
	{
		q = new LinkedList<Object>();
	}

	public void enqueue(Object element)
	{
		synchronized(q) {
			q.add(element);
			q.notify();
		}
	}

	public void enqueue(int index, Object element)
	{
		synchronized(q) {
			q.add(index, element);
			q.notify();
		}
	}

	public Object get(int index) throws InterruptedException
	{
		synchronized(q) {
			while (q.isEmpty())
				q.wait();
			return q.get(index);
		}
	}

	public Object dequeue() throws InterruptedException
	{
		synchronized(q) {
			while (q.isEmpty())
				q.wait();
			return q.removeFirst();
		}
	}

	public Object dequeue(int index) throws InterruptedException
	{
		synchronized(q) {
			while (q.isEmpty())
				q.wait();
			return q.remove(index);
		}
	}

	public boolean isEmpty()
	{
		return q.isEmpty();
	}

	public int size()
	{
		return q.size();
	}
}
